var e = require("../../api.js"),
  stone = require("../../api.js"),
  t = getApp(),
  a = {
    init: function(a, o) {
      var s = this;
      s.page = a, t = o, s.page.orderPay = function(a) {
        function o(e, a, o) {
          t.request({
            url: a,
            data: {
              order_id: e,
              pay_type: "WECHAT_PAY"
            },
            complete: function() {
              wx.hideLoading()
            },
            success: function(e) {
              0 == e.code && wx.requestPayment({
                timeStamp: e.data.timeStamp,
                nonceStr: e.data.nonceStr,
                package: e.data.package,
                signType: e.data.signType,
                paySign: e.data.paySign,
                success: function(e) {
                  console.log("success"), console.log(e)
                },
                fail: function(e) {
                  console.log("fail"), console.log(e)
                },
                complete: function(e) {
                  "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? wx.redirectTo({
                    url: "/" + o + "?status=1"
                  }) : wx.showModal({
                    title: "提示",
                    content: "订单尚未支付",
                    showCancel: !1,
                    confirmText: "确认",
                    success: function(e) {
                      e.confirm && wx.redirectTo({
                        url: "/" + o + "?status=0"
                      })
                    }
                  })
                }
              }), 1 == e.code && wx.showToast({
                title: e.msg,
                image: "/images/icon-warning.png"
              })
            }
          })
        }

        function i(e, a, o) {
          var s = wx.getStorageSync("user_info");
          wx.showModal({
            title: "当前账户余额：" + s.money,
            content: "是否使用余额",
            success: function(s) {
              s.confirm && (wx.showLoading({
                title: "正在提交",
                mask: !0
              }), t.request({
                url: a,
                data: {
                  order_id: e,
                  pay_type: "BALANCE_PAY"
                },
                complete: function() {
                  wx.hideLoading()
                },
                success: function(e) {
                  0 == e.code && wx.redirectTo({
                    url: "/" + o + "?status=1"
                  }), 1 == e.code && wx.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1
                  })
                }
              }))
            }
          })
        }
        var r = a.currentTarget.dataset.index,
          n = s.page.data.order_list[r],
          c = new Array;
        if (void 0 !== s.page.data.pay_type_list) c = s.page.data.pay_type_list;
        else if (void 0 !== n.pay_type_list) c = n.pay_type_list;
        else if (void 0 !== n.goods_list[0].pay_type_list) c = n.goods_list[0].pay_type_list;
        else {
          var d = {};
          d.payment = 0, c.push(d)
        }
        var g = getCurrentPages(),
          u = g[g.length - 1].route,
          p = e.order.pay_data; - 1 != u.indexOf("pt") ? p = e.group.pay_data : -1 != u.indexOf("miaosha") && (p = e.miaosha.pay_data), 1 == c.length ? (wx.showLoading({
          title: "正在提交",
          mask: !0
        }), 0 == c[0].payment && o(n.order_id, p, u), 3 == c[0].payment && i(n.order_id, p, u)) : wx.showModal({
          title: "提示",
          content: "选择支付方式",
          cancelText: "余额支付",
          confirmText: "微信支付",
          success: function(e) {
            e.confirm ? (wx.showLoading({
              title: "正在提交",
              mask: !0
            }), o(n.order_id, p, u)) : e.cancel && i(n.order_id, p, u)
          }
        })
      }, s.page.order_submit = function(o, i) {
        function r() {
          wx.showLoading({
            title: "正在提交",
            mask: !0
          }), t.request({
            url: n,
            method: "post",
            data: o,
            success: function(e) {
              if (0 == e.code) {
                console.log(this.data.payment, 'stone');
                var pay = this.data.payment;
                if (pay == 0) {
                  t.request({
                    url: stone.coupon.pay_send,
                    data: {},
                    success: function(aa) {
                      if (aa.code == 0) {
                        setTimeout(function() {
                          wx.showModal({
                            title: "提示",
                            content: '获取优惠券成功',
                            showCancel: !1
                          })
                        }, 3000);


                      }
                    }
                  });
                }

                var r = function() {
                  t.request({
                    url: c,
                    data: {
                      order_id: n,
                      pay_type: g,
                      form_id: o.formId
                    },
                    success: function(e) {
                      0 == e.code ? (setTimeout(function() {
                        wx.hideLoading();
                      }, 1e3), "pt" == i ? "ONLY_BUY" == s.page.data.type ? wx.redirectTo({
                        url: d + "?status=2"
                      }) : wx.redirectTo({
                        url: "/pages/pt/group/details?oid=" + n
                      }) : wx.redirectTo({
                        url: d + "?status=1"
                      })) : s.page.showToast({
                        title: e.msg,
                        image: "/images/icon-warning.png"
                      })
                    }
                  })
                };
                setTimeout(function() {
                  s.page.setData({
                    options: {}
                  })
                }, 1);
                var n = e.data.order_id,
                  g = "";
                0 == o.payment ? t.request({
                  url: c,
                  data: {
                    order_id: n,
                    pay_type: "WECHAT_PAY"
                  },
                  success: function(e) {
                    if (0 != e.code) 1 != e.code || s.page.showToast({
                      title: e.msg,
                      image: "/images/icon-warning.png"
                    });
                    else {
                      setTimeout(function() {
                        wx.hideLoading()
                      }, 1e3), wx.requestPayment({
                        timeStamp: e.data.timeStamp,
                        nonceStr: e.data.nonceStr,
                        package: e.data.package,
                        signType: e.data.signType,
                        paySign: e.data.paySign,
                        success: function(e) {
                          "pt" == i ? "ONLY_BUY" == s.page.data.type ? wx.redirectTo({
                            url: d + "?status=2"
                          }) : wx.redirectTo({
                            url: "/pages/pt/group/details?oid=" + n
                          }) : wx.redirectTo({
                            url: d + "?status=1"
                          })
                        },
                        fail: function(e) {},
                        complete: function(e) {
                          "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? "requestPayment:ok" != e.errMsg ? wx.redirectTo({
                            url: d + "?status=-1"
                          }) : void 0 !== s.page.data.goods_card_list && s.page.data.goods_card_list.length > 0 ? s.page.setData({
                            show_card: !0
                          }) : wx.redirectTo({
                            url: d + "?status=-1"
                          }) : wx.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(e) {
                              e.confirm && wx.redirectTo({
                                url: d + "?status=0"
                              })
                            }
                          })
                        }
                      });
                      var t = wx.getStorageSync("quick_list");
                      if (t) {
                        for (var o = t.length, r = 0; r < o; r++)
                          for (var c = t[r].goods, g = c.length, u = 0; u < g; u++) c[u].num = 0;
                        wx.setStorageSync("quick_lists", t);
                        for (var p = wx.getStorageSync("carGoods"), o = p.length, r = 0; r < o; r++) p[r].num = 0, p[r].goods_price = 0, a.setData({
                          carGoods: p
                        });
                        wx.setStorageSync("carGoods", p);
                        var l = wx.getStorageSync("total");
                        l && (l.total_num = 0, l.total_price = 0, wx.setStorageSync("total", l));
                        wx.getStorageSync("check_num");
                        0, wx.setStorageSync("check_num", 0);
                        for (var m = wx.getStorageSync("quick_hot_goods_lists"), o = m.length, r = 0; r < o; r++) m[r].num = 0, a.setData({
                          quick_hot_goods_lists: m
                        });
                        wx.setStorageSync("quick_hot_goods_lists", m)
                      }
                    }
                  }
                }) : 2 == o.payment ? (g = "HUODAO_PAY", r()) : 3 == o.payment && (g = "BALANCE_PAY", r())
              }
              if (1 == e.code) return wx.hideLoading(), void s.page.showToast({
                title: e.msg,
                image: "/images/icon-warning.png"
              })
            }
          })
        }
        var n = e.order.submit,
          c = e.order.pay_data,
          d = "/pages/order/order";
        if ("pt" == i ? (n = e.group.submit, c = e.group.pay_data, d = "/pages/pt/order/order") : "ms" == i && (n = e.miaosha.submit, c = e.miaosha.pay_data, d = "/pages/miaosha/order/order"), 3 == o.payment) {
          var g = wx.getStorageSync("user_info");
          wx.showModal({
            title: "当前账户余额：" + g.money,
            content: "是否确定使用余额支付",
            success: function(e) {
              e.confirm && r()
            }
          })
        } else r()
      }
    }
  };
module.exports = a;